#include"WorkerManager.h"
#include"employee.h"
#include"manager.h"
#include"boss.h"


WorkerManager::WorkerManager()
{
	std::ifstream ifs;
	ifs.open(FILENAME, std::ios::in);
	if (!ifs.is_open())
	{
		//std::cout << "文件不存在" << std::endl;
		this->m_EmpArray = NULL;
		this->m_EmpNum = 0;
		this->m_FileIsEmpty = true;
		ifs.close();
		return;
	}

	char ch;
	ifs >> ch;  
	//eof()是判断ifs是否读取到了文件结束符，读取到为true。创建ch，让ifs进行一次输出到临时变量ch，若ch读取的字符为空，那么ifs接下来就指向文件结束符,eof()为true
	if (ifs.eof())
	{
		//std::cout << "文件为空" << std::endl;
		this->m_EmpArray = NULL;
		this->m_EmpNum = 0;
		this->m_FileIsEmpty = true;
		ifs.close();
		return;
	}

	this->m_EmpNum = 0;
	this->m_EmpArray = NULL;
	this->NewSpace = 0;
	//文件存在
	int num = this->Get_EmpNum();
	this->m_EmpNum = num;
	//std::cout << "当前职工人数为：" << num << std::endl;

	this->m_EmpArray = new Worker * [this->m_EmpNum];
	this->Init_Emp();
	/*for (int i = 0; i < this->m_EmpNum; i++)
	{
		std::cout << "职工编号：" << this->m_EmpArray[i]->m_Id << " "
			<< "姓名：" << this->m_EmpArray[i]->m_Name << " "
			<< "部门编号：" << this->m_EmpArray[i]->m_DeptId << std::endl;
	}*/
}

void WorkerManager::Add_Emp()
{
	std::cout << "请输入需要添加的职工数量：" << std::endl;
	int addNum = 0;
	std::cin >> addNum;
	if (addNum > 0)
	{
		int NewSize = this->m_EmpNum + addNum;
		Worker** NewSpace = new Worker * [NewSize+4];
		if (this->m_EmpArray != NULL)
		{
			for (int i = 0; i < this->m_EmpNum; i++)
			{
				NewSpace[i] = this->m_EmpArray[i];
			}
		}
		for (int i = 0; i < addNum; i++)
		{
			int id;
			std::string name;
			int Select;
			std::cout << "请输入第" << i + 1 << "个职工编号" << std::endl;
			std::cin >> id;
			std::cout << "请输入第" << i + 1 << "个职工姓名" << std::endl;
			std::cin >> name;
			std::cout << "请输入该职工工作岗位" << std::endl;
			std::cout << "1.普通职工" << std::endl;
			std::cout << "2.经理" << std::endl;
			std::cout << "3.老板" << std::endl;
			std::cin >> Select;

			Worker* worker = NULL;
			switch (Select)
			{
			case 1:
				worker = new Employee(id, name, 1);
				break;
			case 2:
				worker = new Manager(id, name, 2);
				break;
			case 3:
				worker = new Boss(id, name, 3);
				break;
			default:
				std::cout << "输入有误" << std::endl;
				break;
			}
			NewSpace[this->m_EmpNum + i] = worker;
		}
		delete[] this->m_EmpArray;
		this->m_EmpArray = NewSpace;
		this->m_EmpNum = this->m_EmpNum + addNum;
		this->m_FileIsEmpty = false;
		std::cout << "已成功添加" << addNum << "名职工" << std::endl;
		this->Save();
	}
	else
	{
		std::cout << "输入数量有误" << std::endl;
	}
	system("pause");
	system("cls");
}

void WorkerManager::Save()
{
	std::ofstream ofs;
	ofs.open(FILENAME, std::ios::out);
	for (int i = 0; i < this->m_EmpNum; i++)
	{
		ofs << this->m_EmpArray[i]->m_Id << " "
			<< this->m_EmpArray[i]->m_Name << " "
			<< this->m_EmpArray[i]->m_DeptId << std::endl;
	}
	ofs.close();
}

int WorkerManager::Get_EmpNum()
{
	std::ifstream ifs;
	ifs.open(FILENAME, std::ios::in);
	int Id;
	std::string Name;
	int Did;
	int Num = 0;

	while (ifs >> Id && ifs >> Name && ifs >> Did)
	{
		Num++;
	}
	return Num;
}
void WorkerManager::Init_Emp()
{
	std::ifstream ifs;
	ifs.open(FILENAME, std::ios::in);
	int Id;
	std::string Name;
	int Did;
	int Index = 0;

	while (ifs >> Id && ifs >> Name && ifs >> Did)
	{
		Worker* worker=NULL;
		if (Did == 1)
		{
			worker = new Employee(Id, Name, Did);
		}
		else if (Did == 2)
		{
			worker = new Manager(Id, Name, Did);
		}
		else if (Did == 3)
		{
			worker = new Boss(Id, Name, Did);
		}
		this->m_EmpArray[Index] = worker;
		Index++;
	}
	ifs.close();
}
void WorkerManager::Show_Emp()
{
	if (this->m_FileIsEmpty)
	{
		std::cout << "文件为空或者不存在" << std::endl;
	}
	else
	{
		for (int i = 0; i < this->m_EmpNum; i++)
		{
			this->m_EmpArray[i]->ShowId();
		}
	}
	system("pause");
	system("cls");
}

void WorkerManager::Del_Emp()
{
	
	if (this->m_FileIsEmpty)
	{
		std::cout << "文件不存在或者为空" << std::endl;
	}
	else
	{
		std::cout << "请输入需要删除的职工编号：" << std::endl;
		int id = 0;
		std::cin >> id;
		int index = this->IsExist(id);

		if (index != -1)
		{
			for (int i = index; i < this->m_EmpNum - 1; i++)
			{
				this->m_EmpArray[i] = this->m_EmpArray[i + 1];
			}
			this->m_EmpNum--;
			this->Save();
			std::cout << "删除成功" << std::endl;
		}
		else
		{
			std::cout << "删除失败，未找到该职工" << std::endl;
		}
	}
	system("pause");
	system("cls");
}

int WorkerManager::IsExist(int id)
{
	int index = -1;
	for (int i = 0; i < this->m_EmpNum; i++)
	{
		if (this->m_EmpArray[i]->m_Id == id)
		{
			index = i;
			break;
		}
	}
	return index;
}

void WorkerManager::Mod_Emp()
{
	if (this->m_FileIsEmpty)
	{
		std::cout << "文件为空或者不存在" << std::endl;
	}
	else
	{
		std::cout << "请输入需要修改的职工编号：" << std::endl;
		int id;
		std::cin >> id;
		int ret = this->IsExist(id);
		if (ret!=-1)
		{
			delete this->m_EmpArray[ret];
			std::cout << "找到此人，请输入该职工新的信息：" << std::endl;
			std::cout << "职工编号：" << std::endl;
			int NewId;
			std::cin >> NewId;
			std::cout << "职工姓名：" << std::endl;
			std::string NewName = "";
			std::cin >> NewName;
			std::cout << "职工岗位：" << std::endl;
			int NewDepId;
			std::cin >> NewDepId;
			Worker* worker = NULL;
			switch (NewDepId)
			{
			case 1:
				worker = new Employee(NewId, NewName, NewDepId);
				break;
			case 2:
				worker = new Manager(NewId, NewName, NewDepId);
				break;
			case 3:
				worker = new Boss(NewId, NewName, NewDepId);
				break;
			default:
				std::cout << "输入有误" << std::endl;
				break;
			}
			this->m_EmpArray[ret] = worker;
			this->Save();
			std::cout << "修改成功" << std::endl;
		}
		else
		{
			std::cout << "系统未有此人信息" << std::endl;
		}
		system("pause");
		system("cls");
	}
}

void WorkerManager::Find_Emp()
{
	if (this->m_FileIsEmpty)
	{
		std::cout << "文件不存在或者记录为空" << std::endl;
	}
	else
	{
		std::cout << "请输入查找方式：" << std::endl;
		std::cout << "1.按照职工编号查找：" << std::endl;
		std::cout << "2.按照职工姓名查找：" << std::endl;
		int select;
		std::cin >> select;
		switch (select)
		{
		case 1:
		{//按照编号查找
			int id = 0;
			std::cout << "请输入查找的职工编号：" << std::endl;
			std::cin >> id;
			int ret = IsExist(id);
			if (ret != -1)
			{
				std::cout << "该职工的信息为：" << std::endl;
				this->m_EmpArray[ret]->ShowId();
			}
			else
			{
				std::cout << "查无此人" << std::endl;
			}
		}
			break;
		case 2:
		{
			std::string name;
			std::cout << "请输入查找的职工姓名：" << std::endl;
			std::cin >> name;
			bool flag=false;
			for (int i = 0; i < this->m_EmpNum; i++)
			{
				if (this->m_EmpArray[i]->m_Name == name)
				{
					std::cout << "该职工的信息为：" << std::endl;
					this->m_EmpArray[i]->ShowId();
					flag = true;
				}
			}
			if (flag == false)
			{
				std::cout << "查无此人" << std::endl;
			}
		}
			break;
		default:
			std::cout << "输入有误" << std::endl;
			break;
		}
	}
	system("pause");
	system("cls");
}

void WorkerManager::Sort_Emp()
{
	if (this->m_FileIsEmpty)
	{
		std::cout << "文件不存在或者为空" << std::endl;
		system("pause");
		system("cls");
	}
	else
	{
		std::cout << "请输入排序方式：" << std::endl;
		std::cout << "1.按照职工号升序" << std::endl;
		std::cout << "2.按照职工号降序" << std::endl;
		int Select;
		std::cin >> Select;

		for (int i = 0; i < this->m_EmpNum; i++)
		{
			int MinOrMax = i;
			for (int j = i+1; j < this->m_EmpNum; j++)
			{
				if (Select == 1)
				{
					if (this->m_EmpArray[MinOrMax]->m_Id > this->m_EmpArray[j]->m_Id)
					{
						MinOrMax = j;
					}
				}
				if (Select == 2)
				{
					if (this->m_EmpArray[MinOrMax]->m_Id < this->m_EmpArray[j]->m_Id)
					{
						MinOrMax = j;
					}
				}
			}
			if (i!= MinOrMax)
			{
				Worker* temp = this->m_EmpArray[i];
				this->m_EmpArray[i] = this->m_EmpArray[MinOrMax];
				this->m_EmpArray[MinOrMax] = temp;
			}
		}
		std::cout << "排序后的结果为：" << std::endl;
		this->Save();
		this->Show_Emp();
	}
}

void WorkerManager::CleanFile()
{
	std::cout << "确定清空？" << std::endl;
	std::cout << "1.确定" << std::endl;
	std::cout << "2.返回" << std::endl;
	int select;
	std::cin >> select;

	if (select == 1)
	{
		std::ofstream ofs(FILENAME, std::ios::trunc);  //删除文件后重新创建
		ofs.close();
		if (this->m_EmpArray != NULL)
		{
			
			for (int i = 0; i < this->m_EmpNum; i++)
			{
				delete this->m_EmpArray[i];
				this->m_EmpArray[i] = NULL;
			}
			delete[] this->m_EmpArray;
			this->m_EmpNum = 0;
			this->m_FileIsEmpty = true;
		}
		std::cout << "清空成功，按下仍以键返回" << std::endl;
	}
	system("pause");
	system("cls");
}

void WorkerManager::ShowMenu()
{
	std::cout << "************************" << std::endl;
	std::cout << "  欢迎使用职工管理系统 " << std::endl;
	std::cout << "  1.增加职工信息" << std::endl;
	std::cout << "  2.显示职工信息" << std::endl;
	std::cout << "  3.删除离职职工" << std::endl;
	std::cout << "  4.修改职工信息" << std::endl;
	std::cout << "  5.查找职工信息" << std::endl;
	std::cout << "  6.按照编号排序" << std::endl;
	std::cout << "  7.清空所有文档" << std::endl;
	std::cout << "  0.退出职工系统" << std::endl;
	std::cout << "************************" << std::endl;
	std::cout<<std::endl;
}
void WorkerManager::ExitSystem()
{
	std::cout << "欢迎下次使用" << std::endl;
	system("pause");
	exit(0);
}
WorkerManager::~WorkerManager()
{
	if (this->m_EmpArray != NULL)
	{
		for (int i = 0; i < this->m_EmpNum; i++)
		{
			if (this->m_EmpArray[i] != NULL)
			{
				delete this->m_EmpArray[i];
			}
		}
		delete[] m_EmpArray;
		this->m_EmpArray = NULL;
	}
}